package com.changhong.data.controller;

import com.changhong.data.service.ImageProcessingService;
import com.changhong.data.service.QueryStringAnalysisService;
import com.changhong.data.entity.StreamEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2017/4/6.
 */
@Controller
@RequestMapping("/file")
@ResponseBody
public class DownloadnController {
    @Autowired
    private ImageProcessingService imageProcessingService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private QueryStringAnalysisService queryStringAnalysisService;
    @RequestMapping("/download")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response){
        Map<String,String> params=new HashMap<String, String>();
        params=queryStringAnalysisService.analysis(request);
        String sql="select filename,data from uploadfiles where path=? and user=?";
        //dataEntity dataEntity=new dataEntity();
        StreamEntity streamEntity=new StreamEntity();
        Object[] param={params.get("path"),params.get("user")};
        byte[] bytes=null;
        List<Map<String,Object>> items=jdbcTemplate.queryForList(sql,param);

        for(int i=0;i<items.size();i++){
            streamEntity.setFilename((String)items.get(i).get("filename"));
            streamEntity.setData(new ByteArrayInputStream((byte[]) items.get(i).get("data")));
        }
        if(imageProcessingService.isPicture(streamEntity.getFilename())){
            response.setHeader("Content-Disposition","attachment;filename="+"scale_"+streamEntity.getFilename());}
        else{
            response.setHeader("Content-Disposition","attachment;filename="+streamEntity.getFilename());

        }

        InputStream is=null;

        OutputStream bos=null;

        try {
            is= streamEntity.getData();
            bos=new BufferedOutputStream(response.getOutputStream());
            if(!imageProcessingService.isPicture(streamEntity.getFilename())) {
                byte[] buffer = new byte[2048];
                int num = 0;
                while (-1 != (num = is.read(buffer, 0, buffer.length))) {
                    bos.write(buffer, 0, buffer.length);
                }
            }else {
                bos=imageProcessingService.scale(is,bos,466,320);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }
    @RequestMapping("/scaleimage")
    public void getscaleImage(HttpServletRequest request, HttpServletResponse response){
        Map<String,String> params=new HashMap<String, String>();
        params=queryStringAnalysisService.analysis(request);
        String sql="select filename,data from uploadfiles where path=? and user=?";
        //dataEntity dataEntity=new dataEntity();
        StreamEntity streamEntity=new StreamEntity();
        Object[] param={params.get("path"),params.get("user")};
        byte[] bytes=null;
        List<Map<String,Object>> items=jdbcTemplate.queryForList(sql,param);

        for(int i=0;i<items.size();i++){
            streamEntity.setFilename((String)items.get(i).get("filename"));
            streamEntity.setData(new ByteArrayInputStream((byte[]) items.get(i).get("data")));
        }
        if(imageProcessingService.isPicture(streamEntity.getFilename())){
            response.setHeader("Content-Disposition","attachment;filename="+"scale_"+streamEntity.getFilename());}
        else{
            throw new RuntimeException("Requested file is not an image");

        }

        InputStream is=null;

        OutputStream bos=null;

        try {
            is= streamEntity.getData();
            bos=new BufferedOutputStream(response.getOutputStream());
            bos=imageProcessingService.scale(is,bos,466,320);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }
}

